# _*_ encoding:utf-8 _*_

from django.contrib.auth.hashers import make_password
from rest_framework import serializers

from users.models import User


class AdminModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = [
            'id',
            'username',
            'email',
            'mobile',

            'password',
            
            'groups',  # 新建用户从属分组
            'user_permissions',  # 新建用户拥有的权限
        ]

        extra_kwargs = {
                    'password': {'write_only': True}
                }

    def validate(self, attrs):
        # 密码加密，is_staff=True
        attrs['is_staff'] = True
        raw_password = attrs.get('password') # 明文
        attrs['password'] = make_password(raw_password) # 密文
        return attrs


from rest_framework.permissions import IsAdminUser